package com.iyundao.io.mapper;

import com.iyundao.io.model.IntegralRecord;
import com.iyundao.io.model.IntegralRecordShow;
import com.iyundao.io.model.IntegralShow;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IntegralRecordMapper {
    @Insert({
        "insert into integralrecord (id, partymemberid, ",
        "integralrecordnum, integralrecordtime, ",
        "activityid, giftid, ",
        "userid)",
        "values (#{id,jdbcType=INTEGER}, #{partymemberid,jdbcType=INTEGER}, ",
        "#{integralrecordnum,jdbcType=INTEGER}, #{integralrecordtime,jdbcType=TIMESTAMP}, ",
        "#{activityid,jdbcType=INTEGER}, #{giftid,jdbcType=INTEGER}, ",
        "#{userid,jdbcType=VARCHAR})"
    })
    int insert(IntegralRecord record);

    @InsertProvider(type= IntegralRecordSqlProvider.class, method="insertSelective")
    int insertSelective(IntegralRecord record);

    @Select("select i.id,i.partymemberid,i.integralrecordnum,i.activityid,i.giftid,i.userid,DATE_FORMAT(i.integralrecordtime,'%Y-%m-%d %H:%i:%s') integralrecordtime,gift.giftname,activitymodel.activitytitle from integralrecord i " +
            " left join activitymodel " +
            " on activitymodel.id=i.activityid " +
            " left join gift " +
            " on gift.id = giftid where i.partymemberid=#{partymemberid,jdbcType=INTEGER} order by i.id desc")
    List<IntegralRecordShow> selectByPartyMemberId(Integer partymemberid);

    @Select("select i.id,i.partymemberid,i.integralrecordnum,i.activityid,i.giftid,i.userid,DATE_FORMAT(i.integralrecordtime,'%Y-%m-%d %H:%i:%s') integralrecordtime,activitymodel.activitytitle,gift.giftname from integralrecord i " +
            " inner join activitymodel " +
            " on activitymodel.id = i.activityid " +
            " inner join gift " +
            " on gift.id = i.giftid where i.partymemberid=#{partymemberid,jdbcType=INTEGER} order by i.id desc")
    List<IntegralRecordShow> selectById(Integer partymemberid);

    @Select("select i.id,i.partymemberid,i.integralrecordnum,i.activityid,i.giftid,i.userid,DATE_FORMAT(i.integralrecordtime,'%Y-%m-%d %H:%i:%s') integralrecordtime,gift.giftname,giftintegral from integralrecord i " +
            "LEFT join gift on gift.id = giftid " +
            "where not ISNULL(i.giftid) " +
            "and i.partymemberid=#{partymemberid,jdbcType=INTEGER} order by i.id desc")
    List<IntegralRecordShow> selectExchange(Integer partymemberid);

    @Select("select ir.integralrecordnum,DATE_FORMAT(ir.integralrecordtime,'%Y-%m-%d %H:%i:%s') integralrecordtime,p.id,p.partymembername,g.giftname,g.giftintegral,sd.real_name realName from integralrecord ir " +
            "INNER JOIN partymember p " +
            "on p.id=ir.partymemberid " +
            "INNER JOIN partymemberorganization po " +
            "on po.partymemberid = p.id " +
            "INNER JOIN sys_dept sd " +
            "on sd.sid = po.organizationid " +
            "INNER JOIN gift g " +
            "on g.id = ir.giftid " +
            "WHERE sid=#{sid,jdbcType=VARCHAR} or parent_id = #{sid,jdbcType=VARCHAR} " +
            "and ir.integralrecordtime >= #{date,jdbcType=VARCHAR} " +
            "and ir.integralrecordtime <= #{date1,jdbcType=VARCHAR} and ISNULL (ir.activityid)")
    List<IntegralShow> selectRecordBySid(String sid,String date,String date1);
}